import { _decorator, Component, Node, Vec2, bits, Vec3, RigidBody, debug, log } from 'cc';
import { Battle } from '../Battle';
import { Utitls } from '../Common/Utils';
import { SortingArchor } from '../Other/SortingArchor';
const { ccclass, property } = _decorator;

@ccclass('LayerManager')
export class LayerManager extends Component {

    @property([String])
    nodesName: string[] = [];
    @property(Number)
    interval: number = 1;
    @property(Number)
    offsetY: number = 1;

    @property(Boolean)
    notTimer: boolean = false;

    private nodes: Array<Node> = [];
    private myPoint: Node = null;

    /*start() {
        const Enemys = Battle.ContentParent.children;
        for (let i = 0; i < Enemys.length; i++) {
            let enemyNode = Enemys[i];
            let nodeName = enemyNode.name;
            let result = this.CheckName(nodeName);
            if (result) {
                this.nodes.push(enemyNode);
            }
        }
        this.myPoint = this.node.getChildByName("Point");
        if (this.myPoint == null) {
            this.myPoint = this.node;
        }
        if (this.notTimer) {
            this.UpdateLayer();
        }
        else {
            let intervals = [0.01, 0.02, 0.03, 0.04];
            let intervalTime = intervals[Utitls.Random(0, intervals.length - 1)];
            this.scheduleOnce(this.OnInit.bind(this), intervalTime);
        }
    }*/

    protected update(dt: number): void {
        const children = this.node.children;
        children.sort(this.Compare);
        children.forEach((child, index) => {
            child.setSiblingIndex(0);
        });
    }

    protected Compare(a:Node, b:Node): number{
        let aPoint = a, bPoint = b;
        let aArchor = a.getComponentInChildren(SortingArchor);
        aPoint = aArchor == null ? a : aArchor.node;
        let bArchor = b.getComponentInChildren(SortingArchor);
        bPoint = bArchor == null ? b : bArchor.node;
        return aPoint.worldPositionY - bPoint.worldPositionY;
    }

    /*OnInit() {
        // let intervals = [0.1, 0.2, 0.3, 0.4, 0.6, 0.8, 1];
        // let intervals = [0.02, 0.04, 0.06, 0.08, 0.1];
        let intervals = [0.01, 0.01];
        this.interval = intervals[Utitls.Random(0, intervals.length - 1)];
        this.schedule(this.UpdateLayer.bind(this), this.interval);
    }

    UpdateLayer() {
        // let target = this.GetMinDistance();
        let target = this.GetTargetDistance();
        if (target) {
            let myPosY = this.node.position.y;
            if (this.myPoint) {
                myPosY = myPosY + this.myPoint.position.y;
            }
            let targetPosY = target.position.y;
            let point = target.getChildByName("Point");
            if (point) {
                targetPosY = target.position.y + point.position.y;
            }

            let targetSiblingIndex = target.getSiblingIndex();
            let mySiblingIndex = this.node.getSiblingIndex();

            if (targetPosY < myPosY) {
                if (targetSiblingIndex < mySiblingIndex) {
                    if (this.node.name == "Player") {
                        Battle.ContentParent.insertChild(this.node, targetSiblingIndex);
                    }
                    else{
                        Battle.ContentParent.insertChild(this.node, targetSiblingIndex);
                    }
                }
            }
            else {
                if (targetSiblingIndex > mySiblingIndex) {
                    // Battle.ContentParent.removeChild(this.node);
                    if (this.node.name == "Player") {
                        Battle.ContentParent.insertChild(this.node.parent, targetSiblingIndex);
                    }
                    else{
                        Battle.ContentParent.insertChild(this.node, targetSiblingIndex);
                    }
                }
            }
        }
    }*/

    /**
   * 最近的节点
   */
    /*GetMinDistance() {
        const worldPos = this.node.getWorldPosition();
        let minDistance = bits.INT_MAX;
        let target: Node = null;
        for (let i = 0; i < this.nodes.length; i++) {
            if (this.nodes[i] != this.node) {
                const distance = Vec2.distance(this.nodes[i].getWorldPosition(), worldPos);
                if (distance < minDistance) {
                    minDistance = distance;
                    target = this.nodes[i];
                }
            }
        }
        return target;
    }

    //特定距离下的所有节点
    GetTargetDistance() {
        const worldPos = this.node.getWorldPosition();
        let minDistance = bits.INT_MAX;
        let target = null;

        let myPosY = this.node.position.y;
        if (this.myPoint) {
            myPosY = myPosY + this.myPoint.position.y;
        }
        // let maxIndex: number = -1;
        for (let i = 0; i < this.nodes.length; i++) {
            let elementNode = this.nodes[i];
            if (elementNode && elementNode.active) {
                if (this.nodes[i] != this.node) {
                    let point = this.nodes[i].getChildByName("Point");
                    let nodePosY = 0;
                    if (point == null) {
                        point = this.nodes[i];
                        nodePosY = point.position.y;
                    }
                    else {
                        nodePosY = this.nodes[i].position.y + point.position.y;
                    }
                    // if (myPosY < nodePosY) {

                    // }
                    const distance = Vec2.distance(point.getWorldPosition(), worldPos);
                    if (distance <= 500 && distance < minDistance) {
                        minDistance = distance;
                        target = this.nodes[i];
                        // let siblingIndex = elementNode.getSiblingIndex();
                        // if (siblingIndex > maxIndex) {
                        //     maxIndex = siblingIndex;
                        //     target = this.nodes[i];
                        // }
                    }
                }
            }
        }
        return target;
    }

    CheckName(nodeName) {
        for (let index = 0; index < this.nodesName.length; index++) {
            const element = this.nodesName[index];
            if (nodeName.includes(element)) {
                return true;
            }
        }
        return false;
    }*/

}